define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'score/orders/index' + location.search,
                    add_url: 'score/orders/add',
                    edit_url: 'score/orders/edit',
                    del_url: 'score/orders/del',
                    multi_url: 'score/orders/multi',
                    import_url: 'score/orders/import',
                    table: 'score_orders',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_sn', title: __('Order_sn'), operate: 'LIKE'},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'good_id', title: __('Good_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'cover',
                            title: __('Cover'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'type', title: __('Type'),
                            searchList: {"1": "积分订单", "2": "抽奖订单"},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'award_type', title: __('Award_type'),
                            searchList: {1: "银币", 2: "金币", 3: "积分", 4: "钻石", 0: "其他"},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'amount', title: __('Amount'), operate: 'BETWEEN'},
                        {
                            field: 'status', title: __('Status'),
                            searchList: {0: "待发放", 1: "已完成"},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'fafang',
                                    text: "已发放",
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    confirm: '确认该订单已发放?',
                                    url: 'score/orders/fafang/id/{ids}',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status === 0) {
                                            return true;
                                        }
                                        return false;
                                    },
                                    refresh: true, //操作完成后是否刷新列表
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
